package com.itheima.xx.web;

import com.itheima.xx.pojo.User;
import com.itheima.xx.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description:
 *
 * @author codermao
 * @date 2023/11/1 14:46
 */
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new User(null, username, password);

        UserServiceImpl userService = new UserServiceImpl();
        User u = userService.login(user);

        if (u == null){
            resp.getWriter().print(false);
        }else {
            resp.getWriter().print(true);
        }
    }
}
